package idx;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;

import datastructure.Querystruct;
import datastructure.sentence;

public class Searcher {
	//String query;
	public static void main(String []args) throws CorruptIndexException, IOException
	{
		Searcher s = new Searcher(Integer.parseInt(args[1]), args[0]);
		s.Search("context", new Querystruct(new sentence("古典小说《水浒传》的作者是谁？"),false));
		s.ShowResult();
		return;
	}
	String idxPath;
	int n;
	public Document[] docs;
	public Searcher(int b, String i)
	{
		//query = a;
		n = b;
		idxPath = i;
	}
	public void Search(String field, Querystruct query) throws CorruptIndexException, IOException
	{
		Vector <String> vimWords = new Vector<String>();
		Vector <String> imWords = new Vector <String>();
		Vector <String> uimWords = new Vector <String>();
		for(int i = 0; i < query.keyword.size(); i++)
		{
			String word = query.keyword.get(i).content;
			String tag = query.keyword.get(i).pos;
			if(tag.startsWith("nr") || tag.startsWith("ns")|| tag.startsWith("nz")|| tag.startsWith("nx"))
				vimWords.add(word);
			//if (tag.startsWith("n"))
				//imWords.add(word);
			else
				uimWords.add(word);
		}
		String search = "(";
		if(vimWords.size() > 0)
		{
			for(int i = 0; i < vimWords.size(); i++)
			{
				if( (i+1) != vimWords.size())
					search += field + "='" + vimWords.elementAt(i) + "'&&";
				else
					search += field + ":'" + vimWords.elementAt(i) + "')";
			}
		}
		if(uimWords.size() != 0 && vimWords.size() != 0)
			search += "&&(";
		else
			search += "";
		for(int i = 0; i < uimWords.size(); i++)
		{
			if( (i+1) != uimWords.size())
				search += "context:'" + uimWords.elementAt(i) + "' ||";
			else
				search += "context:'" + uimWords.elementAt(i) + "')";
		}
		System.out.println(search);
		Query q = IKQueryParser.parse(search);
	    int hitsPerPage = n;
	    FSDirectory index = FSDirectory.open(new File(idxPath));
	    IndexSearcher searcher = new IndexSearcher(index, true);
	    searcher.setSimilarity(new IKSimilarity());
	    TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
	    searcher.search(q, collector);
	    ScoreDoc[] hits = collector.topDocs().scoreDocs;
	    docs = new Document[hits.length];
	    System.out.println(hits.length);
	    for(int i = 0; i < hits.length; i++)
	    	docs[i] = searcher.doc(hits[i].doc);
	    
	    return;
	}
	public void ShowResult()
	{
		for(int i = 0; i < docs.length; i++)
			System.out.println(docs[i].get("path"));
		return;
	}
}
